export const validator = {
  // 手机号码正则表达式
  regCellphone: /^1[3-9]\d{9}$/,

  // 邮箱地址正则表达式
  regEmail: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,

  /**
   * 表单内手机号码验证
   * @param rule
   * @param val
   * @param callback
   */
  verifyCellphone: (rule: any, val: string, callback: any) => {
    if (val) {
      if (!validator.regCellphone.test(val)) {
        callback(new Error('请输入有效的手机号码'))
      }
    }
    callback()
  },

  /**
   * 表单内Email验证
   * @param rule
   * @param val
   * @param callback
   */
  verifyEmail: (rule: any, val: string, callback: any) => {
    if (val) {
      if (!validator.regEmail.test(val)) {
        callback(new Error('请输入有效的邮箱地址'))
      }
    }
    callback()
  },

  /**
   * @param {string} path
   * @returns {Boolean}
   */
  isExternal(path: string): boolean {
    return /^(https?:|mailto:|tel:)/.test(path)
  },

  /**
   * 验证手机号码
   *
   * @export
   * @param {*} cellphone
   * @returns
   */
  isCellphone(cellphone: string): boolean {
    return validator.regCellphone.test(cellphone)
  },

  /**
   * 验证邮箱地址
   *
   * @export
   * @param {*} email
   * @returns
   */
  isEmail(email: string): boolean {
    return validator.regEmail.test(email)
  }
}
